package com.atguigu.dga.governance.assessor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


@Component("HAS_SIMILAR_TABLE")
public class HasSimilarTableAssessor extends  Assessor{
    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {
        //1 获取当前表元数据
        TableMetaInfo curTableMetaInfo = assessParam.getTableMetaInfo();
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject paramJsonObj = JSON.parseObject(metricParamsJson);
        BigDecimal paramPercent = paramJsonObj.getBigDecimal("percent");

        //2 获取当前所有表的元数据 //补充
        List<TableMetaInfo> tableMetaInfoList = assessParam.getTableMetaInfoList();

        //3 遍历所有目标表
               //遍历当前表的所有字段
                   //遍历目标表的所有字段
               // 两个表之前有多少字段相同  sameCount
          // sameCount/本表字段数  = similarPercent
        // similarPercent 和paramPercent  对比  如果 超过    similarTableList.add
        List<String> similarTableList=new ArrayList<>();
        for (TableMetaInfo otherTableMetaInfo : tableMetaInfoList) {
            if(!curTableMetaInfo.getTableMetaInfoExtra().getDwLevel().equals(otherTableMetaInfo.getTableMetaInfoExtra().getDwLevel())){
                //非同层次的表跳过
                continue;
            }
            if(curTableMetaInfo.getTableName().equals(otherTableMetaInfo.getTableName())
                && curTableMetaInfo.getSchemaName().equals(otherTableMetaInfo.getSchemaName())){
                //不跟自己比
                continue;
            }
            if(curTableMetaInfo.getTableMetaInfoExtra().getDwLevel().equals("ODS")||otherTableMetaInfo.getTableMetaInfoExtra().getDwLevel().equals("ODS")){
                //不跟ODS的表比
                continue;
            }

            String colNameJson = curTableMetaInfo.getColNameJson();
            List<JSONObject>  curColList= JSON.parseArray(colNameJson,JSONObject.class); //当前表字段
            List<JSONObject>  otherColList= JSON.parseArray(otherTableMetaInfo.getColNameJson(),JSONObject.class); //当前表字段

            int sameCount=0;
            for (JSONObject curColJsonObj : curColList) {
                for (JSONObject otherColJsonObj : otherColList) {
                    if(curColJsonObj.getString("name").equals(otherColJsonObj.getString("name"))){
                        sameCount++;  //相同字段数+1
                    }
                }
            }

            //判断是否为相似表  依据sameCount
            BigDecimal similarPercent=  BigDecimal.valueOf(sameCount).movePointRight(2).divide(BigDecimal.valueOf(curColList.size()),1, RoundingMode.HALF_UP  );


            if (similarPercent.compareTo(paramPercent)>0){
                similarTableList.add(otherTableMetaInfo.getSchemaName()+"."+ otherTableMetaInfo.getTableName());
            }


        }

        //4 如果similarTableList.size>1  则给0分   ， 把相似表输出到问题表述中
        if(similarTableList.size()>0){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("相似表："+ StringUtils.join(similarTableList,",") );
        }

    }
}
